package com.ks.user.service;

/**
 * @author kingdee
 * @date 2020/2/4 14:04
 */

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ks.core.constant.Const;
import com.ks.core.util.DateUtil;
import com.ks.core.util.JsonUtil;
import com.ks.core.util.ToolUtil;
import com.ks.user.entity.BaseSysConfig;
import com.ks.user.entity.MicroApp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务
 */
@Slf4j
@Component
public class QuartzService {

    @Resource
    private IMicroAppService microAppService;
    @Resource
    private IBaseSysConfigService baseSysConfigService;

    /**
     * 每分钟更新  @Scheduled(cron = "0 0/1 * * * ?")
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void updateUser() {
        System.out.println("定时任务执行！！！！！");
        EntityWrapper<MicroApp> wrapper = new EntityWrapper<>();
        MicroApp microApp = new MicroApp();
        microApp.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(microApp);
        List<MicroApp> microApps = microAppService.selectList(wrapper);
        if (ToolUtil.isNotEmpty(microApps)) {
            for (MicroApp app : microApps) {
                long diff = app.getUpdateTime().getTime() - new Date().getTime();
                long days = diff / (1000 * 60 * 60 * 24);
                long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
                long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
                System.out.println(days);
                System.out.println(hours);
                System.out.println(minutes);
                if (minutes <= 5) {
                    try {
                        microAppService.updateMicroApp(app.getAppId(), app.getAuthorizerRefreshToken());
                    } catch (Exception e) {
                        continue;
                    }
                }
            }
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void updateComponentAccessToken() {
        System.out.println("更新第三方平台令牌！");
        Map<String, Object> map = new HashMap<>();
        map.put("component_appid", baseSysConfigService.getComponentAppId());
        map.put("component_appsecret", baseSysConfigService.getComponentAppSecret());
        map.put("component_verify_ticket", baseSysConfigService.getComponentTicket());
        String post = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/component/api_component_token", JsonUtil.mapToJsonStr(map));
        Map<Object, Object> objectObjectMap = JsonUtil.jsonStrToMap(post);
        if (ToolUtil.isNotEmpty(objectObjectMap.get("component_access_token"))) {
            BaseSysConfig baseSysConfig = baseSysConfigService.selectById(Const.Wx.COMPONENT_TOKEN);
            baseSysConfig.setConfigValue(objectObjectMap.get("component_access_token").toString());
            baseSysConfig.setUpdateTime(new Date());
            baseSysConfigService.updateById(baseSysConfig);
            BaseSysConfig config = baseSysConfigService.selectById(6);
            config.setConfigValue(objectObjectMap.get("expires_in").toString());
            config.setUpdateTime(new Date());
            baseSysConfigService.updateById(config);
            BaseSysConfig sysConfig = baseSysConfigService.selectById(3);
            sysConfig.setConfigValue(DateUtil.getTimess(DateUtil.getAfterMinuteDate(new Date(), 7200 / 60)));
            sysConfig.setUpdateTime(new Date());
            baseSysConfigService.updateById(sysConfig);
        }
    }
}
